package com.chijun.mform.create.control;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.chijun.mform.create.mode.FormConfig;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormFieldOption;
import com.example.ccj.R;
import com.example.ccj.b.c.custom.MImageView;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.b.c.recycler.BaseRecycler;
import com.example.ccj.b.c.ui.install.AddOneOptionDialog;
import com.example.ccj.u.GenerateId;

import java.util.List;

/**
 * 展示字段的选项列表的view
 */
public class FieldAddOptionView extends LinearLayout {

    MTextView textView;
    MImageView addOption;
    BaseRecycler<FormFieldOption> recycler;

    FormField formField;
    List<FormFieldOption> formFieldOptionList;


    public FieldAddOptionView(Context context) {
        super(context);
        init();
    }

    public FieldAddOptionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.control_add_option_view,this);
        textView = findViewById(R.id.control_add_option_view_title);
        recycler = findViewById(R.id.control_add_option_view_rec);
        addOption = findViewById(R.id.control_add_option_view_add);

        //添加选项按钮弹出输入选项名称dialog
        addOption.setOnClickListener(v -> {
            AddOneOptionDialog addOneOptionDialog = new AddOneOptionDialog(v.getContext(), (value, val) -> addOneData(value));
            addOneOptionDialog.show();
        });
    }

    public void setData(FormField formField, FormConfig formConfig){
        this.formField = formField;
        this.formFieldOptionList = formConfig.getFormFieldOptionMap().get(formField.getId());
        recycler.setAdapter(formFieldOptionList,new FieldAddOptionAdapter(formField.getId(),formConfig));
    }

    private void addOneData(String val){
        FormFieldOption formFieldOption = new FormFieldOption(GenerateId.getId(),formField.getId(),val);
        recycler.addDataOnTail(formFieldOption);
    }

    public void setTitleName(String val){
        textView.setText(val);
    }



}
